/*
 * @author Brandon Wong
 */
package com.apps.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class WrapperUtils {

	public static final String DEPARTMENTS_URL= "https://courses.students.ubc.ca/cs/main?pname=subjarea&tname=subjareas&req=0";
	public static final String COURSES_URL= "https://courses.students.ubc.ca/cs/main?pname=subjarea&tname=subjareas&req=1&dept=<DEPARTMENT>";
	public static final String SECTIONS_URL= "https://courses.students.ubc.ca/cs/main?pname=subjarea&tname=subjareas&req=3&dept=<DEPARTMENT>&course=<COURSE>";
	public static final String COURSE_INFO_URL= "https://courses.students.ubc.ca/cs/main?pname=subjarea&tname=subjareas&req=5&dept=<DEPARTMENT>&course=<COURSE>&section=<SECTION>";
	public static final String RMP_SEARCH_URL = "http://ratemyprofessors.com/SearchProfs.jsp?letter=<prof last name>";
	public static final String RMP_INFO_URL = "http://www.ratemyprofessors.com/ShowRatings.jsp?tid=<tid>";
	public static final String GOOGL_DIRECTION_URL="http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false";
	public static final String FINAL_EXAMS_URL = "http://www.students.ubc.ca/coursesreg/exams/exam-schedule/";

	
	// @Brandon: Kevin, i see the getURLContentReader is used for all wrapper so I am just going to include
	// 			 it in the utils class.
	//			
	public static BufferedReader getURLContentReader(String url)
			throws MalformedURLException, IOException {
		return new BufferedReader(new InputStreamReader(
				new URL(url).openStream()));
	}

	//  @Brandon: I have tested this function and it works fine..
	//				do you have any test cases for your wrapper so that i can test it after refactoring?
	//
	public static String stringInBwtn(String searchStart, String searchEnd,
			String searchedLine) {

		String result = "";
		if (searchedLine != null) {
			if (searchedLine.contains(searchEnd)
					&& searchedLine.contains(searchStart)) {
				int begin = searchedLine.indexOf(searchStart)+searchStart.length();
				int end = begin + searchedLine.substring(begin).indexOf(searchEnd);
				if(begin < end)
					result = searchedLine.substring(begin,end);
//				result = searchedLine.split(searchStart)[1].split(searchEnd)[0];
			}
		}
		return result;

	}
	
	public static String removeHTML(String in){
		if (in != null) {
		String out = in;
			while (out.contains(">") && out.contains("<")) {
				String remove = out.substring(out.indexOf("<"),
						out.indexOf(">") + 1);
				out = out.replace(remove, "");
			}
			return out;
		}
		return in;
	}
	
	public static String trimAll(String in){
		if( in != null)
			return in.trim().replaceAll("&nbsp;", "");
		return in;
	}

	
	public static void main(String BrandonTheGreat[]) {
		System.out
				.println(WrapperUtils
						.removeHTML(	"<a href=/cs/main?pname=s><asdf>ubjarea<asdf><&tname=subjareas&req=1&dept=ADHE>ADHE</a>"));
	}
}
